package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.project.music.bean.SongList;
import com.project.music.service.SongListService;
import com.project.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 歌单管理控制类
 */

@Api(tags = "歌单管理控制类")
@RestController
@RequestMapping("/songList")
public class SongListController {

    @Autowired
    private SongListService songListService;

    //添加歌单
    @ApiOperation("添加歌单")
    @PostMapping("/add")
    public JSONObject addSongList(SongList songList) {
        JSONObject jsonObject = new JSONObject();
        boolean status = songListService.save(songList);

        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "添加成功");
        }
        else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "添加失败");
        }
        return jsonObject;
    }
    //返回所有歌单
    @ApiOperation("返回所有歌单")
    @GetMapping("")
    public List<SongList> allSongList() {
        return songListService.list();
    }
    //返回指定标题对应的歌单
    @ApiOperation("返回指定标题对应的歌单")
    @GetMapping("/title/detail")
    public List<SongList> songListOfTitle(@RequestParam("title") @ApiParam("歌单标题") String title) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(SongList::getTitle, title);
        return songListService.list(queryWrapper);
    }
    //返回包含指定标题的歌单
    @ApiOperation("返回包含指定标题的歌单")
    @GetMapping("/likeTitle/detail")
    public List<SongList> songListOfLikeTitle(@RequestParam("title") @ApiParam("歌单标题") String title) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .like(SongList::getTitle, title);
        return songListService.list(queryWrapper);
    }
    //返回指定类型的歌单
    @ApiOperation("返回指定类型的歌单")
    @GetMapping("/style/detail")
    public List<SongList> songListOfStyle(@RequestParam("style") @ApiParam("歌单风格") String style) {
        QueryWrapper<SongList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .like(SongList::getStyle, style);
        return songListService.list(queryWrapper);
    }
    //删除歌单
    @ApiOperation("删除歌单")
    @GetMapping("/delete")
    public Boolean deleteSongList(@RequestParam("id") @ApiParam("歌单ID") int id) {
        SongList songList = songListService.getById(id);
        String picPath = songList.getPic();
        if (!picPath.equals("/img/songListPic/123.jpg")) {
            File picFile = new File(Consts.ROOT + picPath);
            boolean picDelete = picFile.delete();
            return songListService.removeById(id) && picDelete;
        } else {
            return songListService.removeById(id);
        }
    }
    //更新歌单信息
    @ApiOperation("更新歌单信息")
    @PostMapping("/update")
    public JSONObject updateSongListMsg(SongList songList) {
        JSONObject jsonObject = new JSONObject();
        boolean status = songListService.updateById(songList);

        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
        }
        else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }
    //更新歌单图片
    @ApiOperation("更新歌单图片")
    @PostMapping("/img/update")
    public JSONObject updateSongListPic(@RequestParam("id") @ApiParam("歌单ID") int id,
                                    @RequestParam("file") @ApiParam("歌单图片") MultipartFile file) {
        JSONObject jsonObject = new JSONObject();
        if (file.isEmpty()) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "图片上传失败");
            return jsonObject;
        }

        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = Consts.ROOT + Consts.SEPARATOR + "img" + Consts.SEPARATOR + "songListPic";
        File path = new File(filePath);
        if (!path.exists()) {
            path.mkdir();
        }

        File dest = new File(filePath + Consts.SEPARATOR + fileName);
        String storePath = "/img/songListPic/" + fileName;
        try {
            file.transferTo(dest);
            UpdateWrapper<SongList> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda()
                    .eq(SongList::getId, id)
                    .set(SongList::getPic, storePath);
            boolean status = songListService.update(updateWrapper);
            if (status) {
                jsonObject.put(Consts.CODE, 1);
                jsonObject.put(Consts.MSG, "上传成功");
            }
            else {
                jsonObject.put(Consts.CODE, 0);
                jsonObject.put(Consts.MSG, "上传失败");
            }
            return jsonObject;
        } catch (IOException e) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败" + e.getMessage());
            return jsonObject;
        }
    }
}
